package com.feng.tiku.service.impl;

import cn.hutool.core.lang.Opt;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.common.dto.PageDto;
import com.feng.common.exceptions.ServiceException;
import com.feng.common.vo.ApiRest;
import com.feng.common.vo.PageResult;
import com.feng.tiku.entity.Repo;
import com.feng.tiku.mapper.RepoMapper;
import com.feng.tiku.service.RepoService;
import com.feng.tiku.vo.RepoHistogram;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 题库 服务实现类
 * </p>
 *
 * @author feng
 * @since 2023-03-20 11:03:08
 */
@Service
public class RepoServiceImpl extends ServiceImpl<RepoMapper, Repo> implements RepoService {

    @Override
    public PageResult<Repo> listRepo(PageDto<String> pageDto) {
        if (pageDto == null) {
            throw new ServiceException("请求参数异常");
        }
        Integer nowPage = Opt.ofBlankAble(pageDto.getNow()).orElse(1);
        Integer size = Opt.ofBlankAble(pageDto.getSize()).orElse(5);
        Page<Repo> page = new Page<>(nowPage, size);
        //判断条件是否有where条件
        boolean present = Opt.ofBlankAble(pageDto.getWhere()).isPresent();
        if (present) {
            QueryWrapper<Repo> wrapper = new QueryWrapper<>();
            wrapper.lambda().like(Repo::getTitle, pageDto.getWhere());
            baseMapper.selectPage(page, wrapper);
        }else {
            baseMapper.selectPage(page, null);
        }
        return new PageResult<>(page.getTotal(), page.getRecords(), page.getPages());
    }

    @Override
    public ApiRest<List<RepoHistogram>> getHistogram() {
        List<RepoHistogram> histogramList = baseMapper.getHistogram();
        return ApiRest.success(histogramList);
    }
}
